/*
@Time : 2021/8/2 下午6:17
@Author : Mrxuexi
@File : todo
@Software: GoLand
*/
package models

import (
	"ListProject-ver1.0/dao"
	"fmt"
	"gorm.io/gorm"
)


type Todo struct {
	gorm.Model
	Title string `json:"title"`
	Status bool `json:"status"`
}

func CreateATodo(todo *Todo) (err error) {
	result := dao.DB.Create(todo)
	fmt.Println("Affected rows:",result.RowsAffected)
	return result.Error
}

func GetAllTodoList() (todoList []*Todo,err error)   {
	result := dao.DB.Find(&todoList)
	fmt.Println("Find Affected rows:",result.RowsAffected)
	return todoList,result.Error
}
func GetATodo(id string)(todo *Todo,err error)  {
	todo = new(Todo)
	result := dao.DB.Where("id=?",id).First(todo)
	return todo,result.Error
}
func UpdateATodo(todo *Todo)(err error)  {
	result := dao.DB.Save(todo)
	fmt.Println("Update Affected rows:",result.RowsAffected)
	return result.Error
}
func DeleteATodo(id string)(err error){
	result := dao.DB.Delete(&Todo{},id)
	fmt.Println("Delete Affected rows:",result.RowsAffected)
	return	result.Error
}